const {ccclass, property} = cc._decorator;

/**设置界面的控制脚本 */
@ccclass
export default class PopupMgr extends cc.Component {

    /**设置界面的总容器 */
    popupRoot: cc.Node = null;

    /**音效、音乐、解散房间的总容器 */
    settings: cc.Node = null;

    /**申请解散房间界面的总容器 */
    dissolveNotice: cc.Node = null;


    /**解散房间的倒计时 */
    endTime = -1;

    /**谁谁谁同意拒绝的文字信息 */
    extraInfo = '';

    /**显示谁谁谁同意拒绝的文字的Label节点 */
    noticeLabel: cc.Label = null;

    onLoad() {
        if(!cc.vv){
            return;
        }
        
        cc.vv.popupMgr = this;

        this.popupRoot = cc.find('Canvas/popups');
        this.settings = cc.find('Canvas/popups/settings');
        this.dissolveNotice = cc.find('Canvas/popups/dissolve_notice');
        this.noticeLabel = this.dissolveNotice.getChildByName('info').getComponent(cc.Label);

        this.closeAll();
        // 向音乐音效右上角的关闭按钮添加点击事件
        this.addBtnHandler('settings/btn_close');
        // 向申请解散房间按钮添加点击事件
        this.addBtnHandler('settings/btn_sqjsfj');
        // 向同意解散按钮添加点击事件
        this.addBtnHandler('dissolve_notice/btn_agree');
        // 向拒绝解散按钮添加点击事件
        this.addBtnHandler('dissolve_notice/btn_reject');
        // 给主界面的设置按钮添加点击事件
        this.addClickEvent(cc.find('Canvas/btn_settings'), this.node, 'PopupMgr', 'showSettings');

        this.node.on(cc.vv.game_event_map.DISSOLVE_NOTICE, (event) => {
            const data = event.detail;
            this.showDissolveNotice(data);
        });

        this.node.on(cc.vv.game_event_map.DISSOLVE_CANCEL, () => {
            this.closeAll();
        });
    }


    start(){
        if(cc.vv.gameNetMgr.dissolveData){
            this.showDissolveNotice(cc.vv.gameNetMgr.dissolveData);
        }
    }

    /**显示详细的同意拒绝信息 */
    showDissolveNotice(data: cc.dissolveData){
        this.endTime = Date.now() / 1000 + data.time;
        this.extraInfo = '';
        data.states.forEach((state, i) => {
            const name = cc.vv.gameNetMgr.seats[i].name;
            if(state){
                this.extraInfo += '\n[已同意] ' + name;
            }
            else{
                this.extraInfo += '\n[待确认] ' + name;
            }
        });

        this.closeAll();
        this.popupRoot.active = true;
        this.dissolveNotice.active = true;
    }


    /**隐藏所有设置界面 */
    closeAll(){
        this.popupRoot.active = false;
        this.settings.active = false;
        this.dissolveNotice.active = false;
    }


    /**给设置界面的按钮添加点击事件 */
    addBtnHandler(btnName: string){
        const btn = cc.find('Canvas/popups/' + btnName);
        this.addClickEvent(btn, this.node, 'PopupMgr', 'onBtnClicked');
    }


    /**添加点击事件 */
    addClickEvent(node: cc.Node, target: cc.Node, component: string, handler: string){
        const eventHandler = new cc.Component.EventHandler();
        eventHandler.target = target;
        eventHandler.component = component;
        eventHandler.handler = handler;

        const clickEvents = node.getComponent(cc.Button).clickEvents;
        clickEvents.push(eventHandler);
    }


    /**各个按钮的点击事件的处理 */
    onBtnClicked(event: cc.Event){
        this.closeAll();

        const btnName = event.target.name;
        switch(btnName){
            case 'btn_agree':
                cc.vv.net.send(cc.vv.game_event_map.DISSOLVE_AGREE);
                break;
            case 'btn_reject':
                cc.vv.net.send(cc.vv.game_event_map.DISSOLVE_REJECT);
                break;
            case 'btn_sqjsfj':
                cc.vv.net.send(cc.vv.game_event_map.DISSOLVE_REQUEST);
                break;
        }
    }

    /**申请解散房间的点击事件 */
    showDissolveRequest(){
        this.closeAll();
        this.popupRoot.active = true;
    }

    /**设置按钮的点击事件 */
    showSettings(){
        this.closeAll();
        this.popupRoot.active = true;
        this.settings.active = true;
    }

    update(dt: number){
        // 更新倒计时
        if(this.endTime > 0){
            const lastTime = this.endTime - Date.now() / 1000;
            if(lastTime < 0){
                this.endTime = -1;
            }

            const m = Math.floor(lastTime / 60);
            const s = Math.ceil(lastTime - m * 60);

            let str = '';
            if(m > 0){
                str += m + '分';
            }
            this.noticeLabel.string = str + s + '秒后房间将自动解散' + this.extraInfo;
        }
    }
}
